/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BUS.LoaiPhong_BUS;
import BUS.Phong_BUS;
import DTO.LoaiPhong_DTO;
import DTO.Phong_DTO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lenh Ho Xung
 */
public final class LapDanhMucPhong extends javax.swing.JFrame {

    DefaultComboBoxModel cbbModelPhong;
    DefaultTableModel tbModel;

    /**
     * Creates new form LapDanhMucPhong
     */
    //private DefaultTableModel tbModel = new DefaultTableModel();
    public LapDanhMucPhong() {
        initComponents();
        //hiện danh sách phòng
        showdanhsachPhong();
        //hiện thông tin vào combobox loại phòng
        showcbbMaLoaiPhong();
        btXoa_p.setEnabled(false);
        btCapNhat_p.setEnabled(false);


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tfTenPhong = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tfGhiChu = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbbMaLoaiPhong = new javax.swing.JComboBox();
        btCapNhat_p = new javax.swing.JButton();
        btLapPhong_p = new javax.swing.JButton();
        btXoa_p = new javax.swing.JButton();
        tfTenLoaiPhong_p = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePhong = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Lập Danh Mục Phòng");
        setBounds(new java.awt.Rectangle(300, 50, 0, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Ghi Chú");

        tfGhiChu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfGhiChuActionPerformed(evt);
            }
        });

        jLabel3.setText("Tên Phòng");

        jLabel2.setText("Mã Loại Phòng");

        jLabel4.setText("Tên Loại Phòng");

        cbbMaLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbMaLoaiPhongActionPerformed(evt);
            }
        });

        btCapNhat_p.setText("Cập nhật");
        btCapNhat_p.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhat_pActionPerformed(evt);
            }
        });

        btLapPhong_p.setText("Lập Phòng");
        btLapPhong_p.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhong_pActionPerformed(evt);
            }
        });

        btXoa_p.setText("Xóa");
        btXoa_p.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoa_pActionPerformed(evt);
            }
        });

        tfTenLoaiPhong_p.setEditable(false);
        tfTenLoaiPhong_p.setBackground(new java.awt.Color(204, 204, 204));
        tfTenLoaiPhong_p.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfTenLoaiPhong_pActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfTenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbMaLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btLapPhong_p)
                        .addGap(35, 35, 35)
                        .addComponent(btXoa_p, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhat_p))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(38, 38, 38)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfTenLoaiPhong_p, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCapNhat_p, btLapPhong_p, btXoa_p});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTenLoaiPhong_p, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btLapPhong_p)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btCapNhat_p)
                                .addComponent(btXoa_p))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbbMaLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btCapNhat_p, btLapPhong_p, btXoa_p});

        tablePhong.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        tablePhong.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tablePhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tablePhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablePhongMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablePhong);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfGhiChuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfGhiChuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfGhiChuActionPerformed

    private void tfTenLoaiPhong_pActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfTenLoaiPhong_pActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfTenLoaiPhong_pActionPerformed

    private void cbbMaLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbMaLoaiPhongActionPerformed
        // TODO add your handling code here:
        try {
            tfTenLoaiPhong_p.setText(LoaiPhong_BUS.laytenLoaiPhong(cbbMaLoaiPhong.getSelectedItem().toString()));
        } catch (Exception e) {
        }
    }//GEN-LAST:event_cbbMaLoaiPhongActionPerformed

    private void btXoa_pActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoa_pActionPerformed
        // TODO add your handling code here:
        int row = this.tablePhong.getSelectedRow();
        int show = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?", "", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {      
            try {
                String maPhong = tbModel.getValueAt(row, 0).toString();
                String tinhTrang=(String)tbModel.getValueAt(row, 5).toString();
                if(tinhTrang.equals("Đang đặt")){
                    JOptionPane.showMessageDialog(rootPane, "Phòng đang đặt không được xóa!");
                    return;                    
                }
                if(tinhTrang.equals("Đang thuê")){
                    JOptionPane.showMessageDialog(rootPane, "Phòngđang thuê không được xóa!");
                    return;                    
                }
                Phong_BUS.Delete(maPhong);

            } catch (Exception e) {
            }
            showdanhsachPhong();
            btXoa_p.setEnabled(false);
            btCapNhat_p.setEnabled(false);
        }
        else if(show==JOptionPane.NO_OPTION){
            return;
        }
       
    }//GEN-LAST:event_btXoa_pActionPerformed

    private void tablePhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablePhongMouseClicked
        // TODO add your handling code here:

        int row = this.tablePhong.getSelectedRow();
        if (row >= 0) {
            btXoa_p.setEnabled(true);
            btCapNhat_p.setEnabled(true);
        } else {
        }
        String tenPhong = (String) this.tbModel.getValueAt(row, 2).toString();
        String tenloaiPhong = (String) this.tbModel.getValueAt(row, 3).toString();
        String ghiChu = (String) this.tbModel.getValueAt(row, 6).toString();


        tfTenPhong.setText(tenPhong);
        tfTenLoaiPhong_p.setText(tenloaiPhong);
        if (ghiChu == null) {
            tfGhiChu.setText("");
        } else {
            tfGhiChu.setText(ghiChu);
        }

        cbbMaLoaiPhong.setSelectedItem(this.tbModel.getValueAt(row, 1).toString());


    }//GEN-LAST:event_tablePhongMouseClicked

    private void btLapPhong_pActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhong_pActionPerformed
        // TODO add your handling code here:

        String maPhong;
        int row = this.tablePhong.getRowCount();
        try {
            if (row > 0) {
                maPhong = Phong_BUS.taoMaTuDong(tbModel.getValueAt(row - 1, 0).toString(), 2);
            } else {
                maPhong = Phong_BUS.taoMaTuDong("MP0000", 2);
            }
            String tenPhong = tfTenPhong.getText();
            String maloaiPhong = cbbMaLoaiPhong.getSelectedItem().toString();
            String tinhtrang = "Trống";
            String ghiChu = tfGhiChu.getText();

            if (tenPhong.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên phòng!");
                return;
            }
            Phong_DTO p = new Phong_DTO();
            p.setMaPhong(maPhong);
            p.setTenPhong(tenPhong);
            p.setMaloaiPhong(maloaiPhong);
            p.setTinhTrang(tinhtrang);
            p.setGhiChu(ghiChu);

            Phong_BUS.Insert(p);


        } catch (Exception e) {
            System.out.print(e.toString());
        }
        tfTenPhong.setText("");
        tfGhiChu.setText("");
        cbbMaLoaiPhong.setSelectedIndex(0);
        showdanhsachPhong();
    }//GEN-LAST:event_btLapPhong_pActionPerformed

    private void btCapNhat_pActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhat_pActionPerformed
        // TODO add your handling code here:
        int row = this.tablePhong.getSelectedRow();
        Phong_DTO p = new Phong_DTO();
        String maPhong = (String) this.tbModel.getValueAt(row, 0).toString();
        String tenPhong = tfTenPhong.getText();
        String maloaiPhong = cbbMaLoaiPhong.getSelectedItem().toString();
        String tinhtrang = "Trống";
        String ghiChu = tfGhiChu.getText();

        if (tenPhong.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên phòng!");
            return;
        }
        p.setMaPhong(maPhong);
        p.setTenPhong(tenPhong);
        p.setMaloaiPhong(maloaiPhong);
        p.setTinhTrang(tinhtrang);
        p.setGhiChu(ghiChu);
        try {
            Phong_BUS.Update(p);
        } catch (Exception e) {
        }
        showdanhsachPhong();
        btXoa_p.setEnabled(false);
        btCapNhat_p.setEnabled(false);


    }//GEN-LAST:event_btCapNhat_pActionPerformed
    protected void showdanhsachPhong() {
        try {
            tbModel = Phong_BUS.layDanhSachPhong();
            tablePhong.setModel(tbModel);
        } catch (Exception e) {
        }
    }

    protected void showcbbMaLoaiPhong() {

        cbbModelPhong = (DefaultComboBoxModel) cbbMaLoaiPhong.getModel();
        ArrayList<LoaiPhong_DTO> ds = new ArrayList<>();
        try {
            ds = LoaiPhong_BUS.layDanhSachMaLoaiPhong();
        } catch (Exception e) {
        }

        for (LoaiPhong_DTO lp : ds) {
            cbbModelPhong.addElement(lp.getMaloaiPhong());
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());

                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LapDanhMucPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LapDanhMucPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LapDanhMucPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LapDanhMucPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LapDanhMucPhong().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhat_p;
    private javax.swing.JButton btLapPhong_p;
    private javax.swing.JButton btXoa_p;
    private javax.swing.JComboBox cbbMaLoaiPhong;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablePhong;
    private javax.swing.JTextField tfGhiChu;
    private javax.swing.JTextField tfTenLoaiPhong_p;
    private javax.swing.JTextField tfTenPhong;
    // End of variables declaration//GEN-END:variables
}
